jQuery(function() {
	var cookie=  $.AMUI.utils.cookie;
 	var sessionId=(cookie.get("sessionId")==null)?"":cookie.get("sessionId");
 	var factoryId=(cookie.get("factoryid")==null)?"":cookie.get("factoryid");
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		/*if(null == sessionId || "undefined" == sessionId || "" == sessionId) {
			window.location.href = "login.html";
		} else {*/
			$.ajax(locat + "/clientUser/driver_list2?sessionId="+sessionId, {
			type: "POST",
			xhrFields: {
				withCredentials: true,
				useDefaultXhrHeader: false
			},
			crossDomain: true,
			success: function(data, status, xhr) {
				if("error" == data["msg"])
					window.location.href = "login.html";
				else {
					var finallist = ""
					for(i = data["list_driver"].length - 1; i >= 0; i--) {
						finallist += "<tr name='" + data["list_driver"][i]["tracknumber"] + "' class='hid'>" +
						" <td class='am-text-center ' name='cs'>" + data["list_driver"][i]["tracknumber"] + "</td>" +
							"<td class='am-text-center '>" + data["list_driver"][i]["phone"] + "</td>" +
							"<td class='am-text-center  ' >" + data["list_driver"][i]["name"] + "</td>" +
							"  <td class='am-text-center '>" +
							"	<button type=\"button\" class=\"am-btn   am-btn-success am-btn-xs am-round\" onclick='updateDriver(\"" + data["list_driver"][i]["d_id"] + "\")'>修改</button>" +
							"	<button type=\"button\" class=\"am-btn am-fr  am-btn-primary am-btn-xs am-round\" onclick='delDriver(\"" + data["list_driver"][i]["d_id"] + "\")'>删除</button>" +
							"</td>" +
							" </tr>";
					}
					$("#table_driver").append(finallist);

				}

			},
			error: function(data) {
				swal("","服务异常！","error");
				window.location.href=login.html;
			}
		});
		//}
		
		
	});

});


//查询司机
			function serchDriver() {

				var driverNum = $("#driverTrc").val();
				var list = document.getElementsByName("cs");
				var trs = $("tr[class='hid']");
				for(i = 0; i < trs.length; i++) {
					trs[i].style.display = "none"; //这里获取的trs[i]是DOM对象而不是jQuery对象，因此不能直接使用hide()方法 
				}
				var len = list.length;
				for(var i = 0; i < len; i++) {
					//如果字符串中不包含目标字符会返回-1
					if(list[i].innerHTML.indexOf(driverNum) >= 0) {
						$("table tbody tr").filter(":contains('" + list[i].innerHTML + "')").show(); //显示匹配上行的车辆信息
					}
				}

			}




//删除driver
function delDriver(driverid) {
	var cookie=  $.AMUI.utils.cookie;
 	var sessionId=(cookie.get("sessionId")==null)?"":cookie.get("sessionId");
	swal({
		title: "您确定要删除吗？",
		text: "",
		type: "warning",
		showCancelButton: true,
		cancelButtonText: "取消",
		closeOnConfirm: false,
		confirmButtonText: "确定",
		confirmButtonColor: "#ec6c62"
	}, function() {
		$.getJSON("json/locat.json", function(data) {
			var locat = data["url"];
			if(null == sessionId || "undefined" == sessionId || "" == sessionId) {
			window.location.href = "login.html";
		} else {
			$.ajax(locat + "/clientUser/delDriver?driverid=" + driverid+"&sessionId="+sessionId, {
				type: "GET",
				xhrFields: {
					withCredentials: true,
					useDefaultXhrHeader: false
				},
				crossDomain: true,
				success: function(data, status, xhr) {
					if(data["mes"] == "success") {
						swal("Good!", "删除成功", "success"); 
						window.location.href = "tjsj.html";
					} else {
						swal("此司机还有订单，不能删除", "", "error"); 
					}
				},
				error: function(data) {
					swal("","服务异常","error");
				    window.location.href=login.html;
				}
			});
		}
			
		});

	});

}

//修改driver
function updateDriver(driverid) {

	window.location.href = "xgsja.html?driverid=" + driverid;
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}